import { RoomBase } from "./base/RoomBase";
import { RoomPlayerBase } from "./base/RoomPlayerBase";
import { ArrayMap } from "../../common/ArrayMap";
import proto from "../../proto/proto";
import { Protocol } from "../../proto/msg";
import { GameRoom } from "./subgame/GameRoom";


export class RoomMgr {
    /**
     * 房间列表
     * key: 房间种类id
     * value: 房间列表
     */
    private _roomKindMap: ArrayMap<number, RoomBase<RoomPlayerBase>[]> = new ArrayMap();
    /**
     * 房间列表
     * key: 房间id
     * value: 房间
     */
    private _roomMap: ArrayMap<number, RoomBase<RoomPlayerBase>> = new ArrayMap();

    /**
     * 通过房间种类创建房间
     */
    createRoom(kindId: number, roomId: number) {
        let room = null;
        switch (kindId) {
            case 100:
                room = new GameRoom(kindId, roomId);
                break
            default:
                room = new GameRoom(kindId, roomId);
        }
        room.onInit();
        this._roomMap.set(room.roomId, room);
        this.getRoomsByKindId(kindId).push(room);
        return room;
    }

    /**
     * 销毁房间
     */
    destroyRoom(roomId: number) {
        if (this._roomMap.has(roomId)) {
            const room = this._roomMap.get(roomId)!;
            if (room.isRoomCanDestroy()) {
                const kindId = room.kindId;
                this._roomMap.delete(roomId);
                const rooms = this.getRoomsByKindId(kindId)
                rooms.splice(rooms.indexOf(room), 1);
                // 销毁房间
                room.onDestroy();
            }
        }
    }
    /**
     * 房间开始
     */
    startRoom(roomId: number) {
        if (this._roomMap.has(roomId)) {
            const room = this._roomMap.get(roomId)!;
            //判断游戏能否开始
            if (room.isRoomCanStart()) {
                console.log(`房间开始游戏:${room.roomId}`);
                //设置房间状态
                room.setRoomState(proto.RoomStateEnum.PLAYING)
                //设置所有玩家状态
                room.setAllPlayerState(proto.PlayerStateEnum.PLAYING);
                //广播房间状态
                room.broadcastRoomState()
                //广播房间玩家信息
                room.broadcastRoomUserInfos()
                //游戏开始逻辑
                room.onStart()
            }
        }
    }

    /**
     * 获取可进入的房间
     */
    getCanEnterRoom(kindId: number) {
        const rooms = this.getRoomsByKindId(kindId);
        let room = rooms.find(a => a.isRoomCanEnter());
        if (!room) {
            let roomId = 0;
            do {
                roomId = Math.floor(Math.random() * 899999 + 100000)
            } while (this._roomMap.has(roomId))
            room = this.createRoom(kindId, roomId);
        }
        return room;
    }


    /**
     * 通过玩家id 获取房间
     */
    getRoomByUserId(userId: number): RoomBase<RoomPlayerBase> | null {
        for (let i = 0; i < this._roomKindMap.values.length; i++) {
            const rooms = this._roomKindMap.values[i]
            for (let j = 0; j < rooms.length; j++) {
                const room = rooms[j]
                if (room.hasPlayer(userId)) {
                    return room
                }
            }
        }
        return null
    }

    /**
     * 房间类型id 获取房间
     * @param userId 
     */
    getRoomsByKindId(kindId: number): RoomBase<RoomPlayerBase>[] {
        if (!this._roomKindMap.has(kindId)) {
            this._roomKindMap.set(kindId, [])
        }
        return this._roomKindMap.get(kindId)!
    }

    /**
     * 获取所有存在的房间
     * @returns 
     */
    onMsg(userId: number, cmd: number, reqWrap: proto.ReqWrap, reqData: any) {
        this._roomMap.values.forEach(room => room.onMsg(userId, cmd, reqWrap, reqData));
    }
}